package cn.com.customer.manager.ui.order.detail

import cn.com.customer.manager.bean.GoodListInfo
import cn.com.customer.manager.bean.OrderDetailInfo
import cn.com.customer.manager.bean.ReceivePayListInfo
import cn.com.customer.manager.bean.RequestBean
import cn.com.customer.manager.http.BaseApi
import cn.com.customer.manager.http.subscriber.TipRequestSubscriber
import com.xuexiang.xhttp2.XHttp
import com.xuexiang.xhttp2.model.ApiResult

class OrderDetailPresenter constructor(view:OrderDetailContract.View):OrderDetailContract.Presenter{
    var mView:OrderDetailContract.View=view

    override fun getData(params: MutableMap<String, String>) {
        val request = XHttp.custom()
        request.call(request.create(BaseApi.IOrder::class.java)
                .orderDetail(RequestBean(params)))
                .subscribeWith(object : TipRequestSubscriber<ApiResult<OrderDetailInfo>?>() {
                    override fun onSuccess(apiResult: ApiResult<OrderDetailInfo>?) {
                        apiResult?.let {
                            if (it != null) {
                                mView.setData(it.data)
                            }
                        }
                    }
                })
    }

    override fun getGoodList(params: MutableMap<String, String>) {
        val request = XHttp.custom()
        params["pageNo"] = "1"
        params["pageSize"] = "1000"
        request.call(request.create(BaseApi.IOrder::class.java)
                .orderGoodsList(RequestBean(params)))
                .subscribeWith(object : TipRequestSubscriber<ApiResult<GoodListInfo>?>() {
                    override fun onSuccess(apiResult: ApiResult<GoodListInfo>?) {
                        apiResult?.let {
                            if (it != null) {
                                mView.setGoodList(it.data)
                            }
                        }
                    }
                })
    }

    override fun getReceivedPaymentsList(params: MutableMap<String, String>) {
        val request = XHttp.custom()
        request.call(request.create(BaseApi.IOrder::class.java)
                .orderReceivedPaymentsList(RequestBean(params)))
                .subscribeWith(object : TipRequestSubscriber<ApiResult<ReceivePayListInfo>?>() {
                    override fun onSuccess(apiResult: ApiResult<ReceivePayListInfo>?) {
                        apiResult?.let {
                            if (it != null) {
                                mView.setReceivedPaymentsList(it.data)
                            }
                        }
                    }
                })
    }
    override fun receivedPaymentAdd(params: MutableMap<String, String>) {
        val request = XHttp.custom()
            request.call(request.create(BaseApi.IOrder::class.java)
                    .orderReceivedPaymentAdd(RequestBean(params)))
                    .subscribeWith(object : TipRequestSubscriber<ApiResult<Map<String, String>>?>() {
                        override fun onSuccess(apiResult: ApiResult<Map<String, String>>?) {
                            mView.addSuccess()
                        }
                    })

    }
    override fun begin(params: MutableMap<String, String>) {
        getGoodList(params)
        getReceivedPaymentsList(params)
    }

    override fun start() {

    }

}